<template>
  <el-dialog
    :visible.sync="visible"
    append-to-body
    class="dragCanvas_dialog"
    :title="title"
    :close-on-click-modal="false"
    :width="`${width}px`"
    v-dialogDrag="true"
    @closed="handleClosed"
  >
    <iframe
      id="iframe"
      width="100%"
      style="height: 87vh"
      :src="prefix + '/exportModules/'"
      frameborder="1"
    ></iframe>
    <div slot="footer">
      <el-button type="primary" @click="clear" size="small">清空</el-button>
      <el-button type="primary" @click="save" size="small">上传图片</el-button>
      <!-- <el-button type="primary" @click="zancun">暂存</el-button>
      <el-button type="primary" @click="refresh">恢复</el-button> -->
    </div>
  </el-dialog>
</template>

<script>
import { upload } from "@/api/basicData/seal";
export default {
  data() {
    return {
      width: 0,
      visible: false,
      title: "平面图",
      prefix: process.env.VUE_APP_CONTEXT_PATH == "/" ? "" : process.env.VUE_APP_CONTEXT_PATH,
    };
  },
  mounted() {
    console.log('平面设计图组件已加载')
    this.width = document.documentElement.clientWidth;
    window.addEventListener("message", this.listenMessage, true);
  },
  destroyed() {
    console.log('平面设计图组件已销毁')
    window.removeEventListener("message", this.listenMessage, true);
  },
  methods: {
    openDialog(data = {}) {
      this.visible = true;
      setTimeout(() => {
        let otherWindow = document.getElementById("iframe").contentWindow;
        otherWindow.postMessage({
          type: "importData",
          data: JSON.parse(data?.imageJson || ""),
        }, "*");
      }, process.env.NODE_ENV === "production" ? 4200 : 1500);
    },
    listenMessage(event) {
      console.log('设计完毕接收到的数据', event.data)
      const { isDraw, dataUrl, data: imageJson } = event.data;
      if (isDraw) {
        var blob = this.dataURLtoBlob(dataUrl);
        var file = this.blobToFile(blob, "截图.jpg");
        let fd = new FormData();
        fd.append("file", file);
        upload(fd).then((response) => {
          this.visible = false;
          this.$modal.msgSuccess("上传成功");
          response.data.imageJson = JSON.stringify(imageJson || "");
          this.$emit("confirm", response.data);
        }).catch(err => {});
      }
    },
    // 将base64转换为blob
    dataURLtoBlob: function (dataurl) {
      var arr = dataurl.split(",");
      var mime = arr[0].match(/:(.*?);/)[1];
      var bstr = atob(arr[1]);
      var n = bstr.length;
      var u8arr = new Uint8Array(n);
      while (n--) {
        u8arr[n] = bstr.charCodeAt(n);
      }
      return new Blob([u8arr], { type: mime });
    },
    // 将blob转换为file
    blobToFile: function (theBlob, fileName) {
      theBlob.lastModifiedDate = new Date();
      theBlob.name = fileName;
      return theBlob;
    },
    save() {
      let otherWindow = document.getElementById("iframe").contentWindow;
      otherWindow.postMessage({ type: "uploadSave" }, "*");
    },
    // 清空画布 //
    clear() {
      let otherWindow = document.getElementById("iframe").contentWindow;
      otherWindow.postMessage({ type: "clearCanvas" }, "*");
    },
    handleClosed() {
      this.clear();
    },
  },
};
</script>

<style lang="scss" scoped>
.dragCanvas_dialog {
  ::v-deep .el-dialog {
    max-height: 100%;
    height: 100%;
    .el-dialog__body {
      padding: 0;
    }
  }
}
// flex横向布局
.rowflex {
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: center;
}

.group-wrap {
  border-bottom: 1px solid #eee;
}

.type-group {
  font-weight: bold;
  margin-left: 5px;
  margin-top: 10px;
  margin-bottom: 20px;
}

.grid-item {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 10px; /* 格子间隔 */
}

// flex纵向布局
.columnflex {
  display: flex;
  align-items: center;
  justify-content: center;
}

// 主div
.mainDiv {
  height: calc(100vh - 118px) !important;
  width: 100%;
  min-width: 1000px;
  padding: 0 10px;
}

/* 左侧元素区 */
.leftFiv {
  height: 100%;
  width: 20%;
  padding: 10px;
  // 元素块
  .element {
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;
    width: 50px;
    height: 50px;
    min-width: 50px;
    margin-bottom: 20px;
    border: 1px solid #eee;
    cursor: move;
    &:hover {
      box-shadow: 0 0 2px 2px #999;
      transition: all 0.4s;
    }

    .element_item {
      height: 90%;
      width: 90%;
    }
  }
}

/* 右侧画布区 */
.rightDiv {
  flex: 1;
  height: 100%;
  border: 1px solid #999;
}

.menu-x {
  z-index: -100;
  position: absolute;
  top: 0;
  left: 0;
  box-sizing: border-box;
  border-radius: 4px;
  box-shadow: 0 0 4px rgba(0, 0, 0, 0.3);
  background-color: #fff;
}

/* 菜单每个选项 */
.menu-li {
  box-sizing: border-box;
  padding: 4px 8px;
  border-bottom: 1px solid #ccc;
  cursor: pointer;
  line-height: 30px;
  height: 40px;
  width: 120px;
}

/* 鼠标经过的选项，更改背景色 */
.menu-li:hover {
  background-color: antiquewhite;
}

/* 第一个选项，顶部两角是圆角 */
.menu-li:first-child {
  border-top-left-radius: 4px;
  border-top-right-radius: 4px;
}

/* 最后一个选项，底部两角是圆角，底部不需要边框 */
.menu-li:last-child {
  border-bottom: none;
  border-bottom-left-radius: 4px;
  border-bottom-right-radius: 4px;
}
</style>
